import * as $answer from '@/api/answer'
import * as _helper from '@/utils/helper'
import * as _app from '@/utils/app'

import {PROGRESS_UPDATE_PROGRESSES_STATE} from './progress'
import {UPDATE_IS_LOADING} from './ui'

export const ANSWER_SET_ANSWER = 'ANSWER_SET_ANSWER'
export const ANSWER_ADD_ITEM = 'ANSWER_ADD_ITEM'
export const ANSWER_SET_ANSWERS = 'ANSWER_SET_ANSWERS'
export const ANSWER_ADD_COMMENT_ITEM_FOR_ONE_OF_ANSWERS = 'ANSWER_ADD_COMMENT_ITEM_FOR_ONE_OF_ANSWERS'
export const ANSWER_UPLOAD_QUEUE_NUM = 'ANSWER_UPLOAD_QUEUE_NUM'

const state = {
  answer: {},
  answers: [],
  uploadQueueNum: -1,
}

const getters = {
  answer: state => state.answer,
  uploadQueueNum: state => state.uploadQueueNum,
  answers (state) {
    let t = []
    state.answers.forEach(n => {
      let createdAtName = _helper.timeName(n.createdAt)
      n.createdAtName = createdAtName
      t.push(n)
      if (!n.comments) return
      let tt = []
      n.comments.forEach(nn => {
        let createdAtName = _helper.timeName(nn.createdAt)
        nn.createdAtName = createdAtName
        tt.push(nn)
      })
      n.comments = tt
    })
    return t
  },
  isCreateAble (state, gettters, rootState, rootGetters) {
    let pageProgressId = rootGetters['progress/pageProgressId']
    let textBlocks = rootGetters['progress/textBlocks']
    let contentBlock = textBlocks[pageProgressId]
    let mediaBlocks = rootGetters['progress/mediaBlocks']

    let blocks = mediaBlocks[pageProgressId]
    let uploadQueueNum = gettters['uploadQueueNum']
    if ((!contentBlock.value && blocks.length === 0) || uploadQueueNum > 0) return false
    return true
  },
  lastAnswerId (state, gettters, rootState, rootGetters) {
    let id = 0
    let len = state.answers.length
    if (len > 0) {
      id = state.answers[state.answers.length - 1]['id']
    }
    return id
  }
}

const actions = {
  async create ({ dispatch, commit, state, rootState, rootGetters, getters }, progressId) {
    let textBlocks = rootGetters['progress/textBlocks']
    let contentBlock = textBlocks[progressId] || {}
    let mediaBlocks = rootState.progress.mediaBlocks[progressId] || []
    let blocks = _app.getBlocks({ contentBlock, blocks: mediaBlocks })
    dispatch('resetValidateError', null, {root: true})

    if (blocks.length === 0) {
      dispatch('addValidateError', '内容不能为空！', {root: true})
    }
    if (rootState.ui.validateErrors.length > 0) return
    let res = await $answer.create({ progressId, data: {blocks} })
    commit(ANSWER_ADD_ITEM, res)
    dispatch('progress/updateIsShowReplyForm', false, {root: true})
    dispatch('progress/resetTextBlocks', progressId, {root: true})
    dispatch('progress/setMediaBlocks', {progressId, blocks: []}, {root: true})
    commit('progress/' + PROGRESS_UPDATE_PROGRESSES_STATE, {progressId, st: 'done'}, {root: true})
    commit('UI_SET_TOAST_SUCCESS_MSG', '回复成功', {root: true})
  },
  async setUploadQueueNum ({ dispatch, commit, state, rootState, rootGetters, getters }, num) {
    commit(ANSWER_UPLOAD_QUEUE_NUM, num)
  },
}

const mutations = {
  ANSWER_SET_ANSWERS (state, answers) {
    state.answers = answers
  },
  ANSWER_SET_ANSWER (state, answer) {
    state.answer = answer
  },
  ANSWER_ADD_ITEM (state, answer) {
    state.answers.push(answer)
  },
  ANSWER_ADD_COMMENT_ITEM_FOR_ONE_OF_ANSWERS (state, { comment, answerId }) {
    let idx = state.answers.findIndex(n => n.id === answerId)
    state.answers[idx]['comments'].push(comment)
  },
  ANSWER_UPLOAD_QUEUE_NUM (state, num) {
    state.uploadQueueNum = num
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
